<?php
/**
 * @ClassName: Marketing
 * @Description: 营销活动
 * @author: wyaohu
 * @Date: 2024/09/11 19:46
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\enum\CommonEnum;
use app\common\enum\PayStatus;
use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Marketing extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\Marketing();
        $this->pageTitle = '营销活动';
        $this->pagesize = 10;

        $this->indexWith = ['member', 'store'];

        $this->selectTextField = '{id}#{title}';
        $this->selectSearch = 'title';
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;

        $search->select('member_id', '会员')->dataUrl(url("/admin/member/selectPage"));
        $search->select('store_id', '店铺')->dataUrl(url("/admin/store/selectPage"));
        $search->select('goods_id', '商品')->dataUrl(url("/admin/shopgoods/selectPage"));
        $search->text('title', '标题');
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];

        if (isset($searchData['member_id']) && $searchData['member_id'] != '') {
            $where[] = ['member_id', '=', $searchData['member_id']];
        }
        if (isset($searchData['store_id']) && $searchData['store_id'] != '') {
            $where[] = ['store_id', '=', $searchData['store_id']];
        }
        if (isset($searchData['goods_id']) && $searchData['goods_id'] != '') {
            $where[] = ['goods_id', 'in', ',' . $searchData['goods_id'] . ','];
        }
        if (isset($searchData['title']) && $searchData['title'] != '') {
            $where[] = ['title', 'like', '%' . trim($searchData['title']) . '%'];
        }

        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('member_id', '会员')->to('{member_id}#{member.name}');
        $table->show('store_id', '店铺')->to('{store_id}#{store.name}');
        $table->show('title', '标题');
        $table->image('img', '封面');
        $table->show('num', '目标人数');
        $table->show('bucket_num', '首次购买桶数');
        $table->show('prices', '奖励金额');
        $table->match('status', '审核状态')->options(CommonEnum::examineText(true))
            ->mapClassGroup([[0, 'info'], [1, 'success'], [2, 'danger']]);
        $table->match('done', '活动状态')->options([0 => '未开始', 1 => '进行中', 2 => '已结束'])
            ->mapClassGroup([[0, 'info'], [1, 'success'], [2, 'danger']]);
        $table->match('pay_status', '支付状态')->options(dict_data('pay_status'))
            ->mapClassGroup([[-1, 'danger'], [0, 'info'], [1, 'success'], [2, 'info'], [3, 'success'], [4, 'danger'], [5, 'success']]);
        $table->show('remark', '描述');
        $table->fields('times_group', '添加/修改时间')->with(
            $table->show('create_time', '添加时间')->default('---'),
            $table->show('update_time', '修改时间')->default('---')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, create_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnDelete()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $form->hidden('id');
        $form->select('member_id', '会员')->dataUrl(url('/admin/member/selectPage'))->required();
        $form->select('store_id', '店铺')->dataUrl(url('/admin/store/selectPage'))->required();
        $form->select('goods_id', '商品')->dataUrl(url("/admin/shopgoods/selectPage"))->required();
        $form->text('title', '标题')->maxlength(50)->required();
        $form->image('img', '封面')->required();
        $form->number('num', '目标人数')->required();
        $form->number('bucket_num', '首次购买桶数')->required();
        $form->text('prices', '奖励金额')->required();
        $form->textarea('remark', '描述');

        $form->radio('status', '审核状态')->options(CommonEnum::examineText(true))->default(0)
            ->when(2)->with(
                $form->textarea('disallow', '驳回原因')->required()
            )->required();
        $form->radio('done', '活动状态')->options([0 => '未开始', 1 => '进行中', 2 => '已结束'])->default(0)->required();

        if ($isEdit) {
            $form->show('create_time', '添加时间')->default('---');
            $form->show('update_time', '修改时间')->default('---');
        }
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
//        $data = request()->only([], 'post');
        $data = request()->post();

        $result = $this->validate($data, []);

        if (!$result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }

}